package com.cashmama.market.api.application.controller.user;

import com.cashmama.market.api.application.annos.VisitorAccessible;
import com.cashmama.market.api.application.controller.user.request.AccessTokenVO;
import com.cashmama.market.api.application.controller.user.request.SocialBindMobileVO;
import com.cashmama.market.api.domain.handler.order.OrdersHandler;
import com.cashmama.market.api.domain.service.SocialService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.user.profile.AccessTokenDTO;
import com.cashmama.market.api.domain.vo.user.profile.SocialBindMobileDTO;
import com.cashmama.market.api.domain.vo.user.profile.UserRegisterLoginVO;
import com.cashmama.market.api.domain.vo.user.profile.UserSocialLoginResponse;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.handler.ApiResponse;
import com.cashmama.market.api.infras.utils.HttpUtil;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.fqgj.exception.common.ApplicationException;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: market-api
 * @description 社交网络登录
 * @author: frank
 * @create: 2020-04-28 15:11
 **/
@RestController
@RequestMapping("/user/profile/social")
public class SocialController {

    @Autowired
    private ApolloConfigUtil apolloConfigUtil;
    @Autowired
    private SocialService socialService;
    @Autowired
    private OrdersHandler ordersHandler;
    /**
     * 通过社交网络登录
     *
     * @param accessTokenVO
     * @return
     */
    @VisitorAccessible
    @ParamsValidate
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResponse<UserSocialLoginResponse> login(@RequestBody AccessTokenVO accessTokenVO) {

        if(!apolloConfigUtil.getSocialLoginOpen()){
            throw new ApplicationException("Sorry, the current login method is not supported");
        }
        AccessTokenDTO accessToken = new AccessTokenDTO();
        BeanUtils.copyProperties(accessTokenVO, accessToken);
        UserRegisterLoginVO userRegisterLoginVO = socialService.socialLogin(accessToken);
        UserSocialLoginResponse userSocialLoginResponse = new UserSocialLoginResponse(userRegisterLoginVO);
        String appCode = RequestLocalInfo.getRequestBasicInfo().getAppCode();
        String packageName = RequestLocalInfo.getRequestBasicInfo().getPackageName();
        List<ProductCategory> productCategoryList = ordersHandler.getProductCategoryList(packageName, appCode);
        userSocialLoginResponse.setCategoryCode(productCategoryList.get(0).getCode());
        return new ApiResponse<>(userSocialLoginResponse).setMsg("Login Success");
    }

    @ParamsValidate
    @VisitorAccessible
    @RequestMapping(value = "/bind/mobile", method = RequestMethod.POST)
    public ApiResponse<UserSocialLoginResponse> bindMobile(HttpServletRequest request, @RequestBody SocialBindMobileVO socialBindMobileVO) {

        SocialBindMobileDTO socialBindMobile = new SocialBindMobileDTO();
        BeanUtils.copyProperties(socialBindMobileVO, socialBindMobile);
        String ip = HttpUtil.getIpAddr(request);
        UserRegisterLoginVO userRegisterLoginVO = socialService.bindMobile(socialBindMobile, ip);
        UserSocialLoginResponse userSocialLoginResponse = new UserSocialLoginResponse(userRegisterLoginVO);
        return new ApiResponse<>(userSocialLoginResponse);
    }
}
